package observer;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public abstract class Subject{
	protected List<Observer> observers = new LinkedList<Observer>();
	
	public void addObserver(Observer observer){
		if (!observers.contains(observer)){
			observers.add(observer);
		}
	}
	
	public void removeObserver(Observer observer){
		observers.remove(observer);
	}
	
	public void removeObservers(List<Observer> observers){
		for (Observer obs : observers){
			removeObserver(obs);
		}
	}
	
	public List<Observer> getObservers(){
		return observers;
	}
	
	public void setObserversNull(){
		observers = new LinkedList<Observer>();
	}
	
	public void notifyObservers(){
		Iterator<Observer> it = observers.listIterator();
		while (it.hasNext()){
			Observer obs = it.next();
			obs.update(this);			
		}		
 	}
	
	public void notifyObservers(Object event){
		List<Observer> obs = new LinkedList<Observer>();
		obs.addAll(observers);
		Iterator<Observer> it = obs.listIterator();
		while (it.hasNext()){
			Observer o = it.next();
			o.update(event);			
		}		
	}
	
	public void notifyObserversAt(Observer o, Object event){
	/*	List<Observer> obs = new LinkedList<Observer>();
		obs.addAll(observers);
		Iterator<Observer> it = obs.listIterator();
		
		while (it.hasNext()){
			Observer tempobs = it.next();
			if(tempobs.getClass().getCanonicalName()==o.getClass().getCanonicalName())
			System.out.println("Aggiorno Pannello Principale "+tempobs.getClass().toString()+" "+o.getClass().toString());

		
			//if(tempobs.getClass().toString().equals(o.getClass().toString()))
			
			
			tempobs.update(event);			
		}
	*/
	}
	
	
	
	
	
	

}
